<template>
	<view class="page">
		<NavbarHead :toTop="false" :leftTitle="navText" :bgColor="bgColor" v-if="!isHideTabbar"></NavbarHead>
		<view class="topBox" :style="{'padding-top':topHeadHeight+ 'rpx' }">
			<view class="searchBox">
				<view class="area" @click="areaClick">
					<text>{{areaData.name}}</text>
					<image src="http://qiniu.suboshimuying.cn/static/search/arrow.png" mode=""></image>
				</view>
				<view class="search" @click="toUrl('/subPages/search/search')">
					<image src="http://qiniu.suboshimuying.cn/static/search/search.png" style="margin-right: 10rpx;"
						mode=""></image>
					<text style="color: rgba(0,0,0,0.4);">搜索</text>
				</view>
			</view>
			
			<homeSwiper @hideTabbar="isHideTabbar=!isHideTabbar" />
		</view>
		<view class="displayBox">
			<displayBox cate="3" :area="areaData.id" />
			<displayBox cate="2" :area="areaData.id" />
			<displayBox cate="4" :area="areaData.id" />
		</view>
		<view class="listTop">
			<view class="listTop-title" @click="oneIdClick">
				<image src="http://qiniu.suboshimuying.cn/static/home/Frame 2737@2x.png" style="height: 88rpx;" mode="">
				</image>
			</view>

			<scroll-view scroll-x="true" class="listTop-tabs">
				<text v-for="item in tabsList" :key="item.id" :class="{'textActive':item.id ===twoCate}"
					@click="getcateshop(item.id)">{{item.name}}</text>
			</scroll-view>

		</view>
		<view class="listBox">
			<view class="itemBox" v-for="item in dataList" :key="item.id"
				@click="toUrl(`/subPages/goodDetails/goodDetails?type=1&id=${item.id}`)">
				<image class="ship" :src="item.thum_image" mode=""></image>
				<view class="itemBox-name u-line-2">
					{{item.name}}
				</view>
				<view class="itemBox-price">
					某宝参考价:¥{{item.mb_price}}
				</view>
				<view class="itemBox-price">
					某东参考价:¥{{item.md_price}}
				</view>
				<view class="itemBox-addCart">
					<view class="left">
						<view style="color: #EC6666;">
							<text>¥</text>
							<text style="font-size: 32rpx;">{{item.price}}</text>
							<text style="color: rgba(0,0,0,0.26);margin-left: 10rpx;">¥{{item.order_price}}</text>
						</view>
						<view>
							{{item.pay_num}}+人付款
						</view>
					</view>
					<view class="right" @click.stop="addCart(item.id)">
						<image src="http://qiniu.suboshimuying.cn/static/home/ic-shopping-cart@2x.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<template v-if="loadShow">
			<u-divider text="上划加载更多"></u-divider>
		</template>
	</view>
</template>

<script>
	import NavbarHead from '../../components/NavbarHead/NavbarHead.vue'
	import homeSwiper from './homeSwiper.vue'
	import displayBox from './displayBox.vue'
	export default {
		data() {
			return {
				topHeadHeight: 0,
				areaData: {}, //地址信息
				tabsList: [], //tabs列表
				twoCate: null, //二级分类ID
				dataList: [], //商品列表
				last_page: null, // 总页数
				page: 1, //当前页
				loadShow: true, //加载更多
				bgColor: 'rgba(0, 0, 0, 0)',
				isHideTabbar: false, // tabbar 状态
				navText: '苏博士母婴商城'

			}
		},
		watch:{
			isHideTabbar(n){
				console.log(n, 'navbar状态')
				this.navText = '苏博士母婴商城'
			}
		},
		onLoad(option) {
			// 1:热卖 2:本地服务 3:薅羊毛 4:镇店之宝

			// console.log(option);
			if (option.scene) {
				let pid = option.scene.split('%3D')[1]
				uni.setStorageSync('pid', pid)
			}

			if (option.fxuserId && option.fxuserId != uni.getStorageSync('userId')) {
				uni.setStorageSync('fxuserId', option.fxuserId)
			}

			this.topHeadHeight = uni.getStorageSync('headHeight') * 2 || 172

			// if (uni.getStorageSync('token')) {
				// 传经纬度获取当前地址ID
				this.$getLocation((lng, lat) => {
					this.getarea(lng, lat)
					// console.log(e);
				}, (err) => {
					// console.log(err);
					this.$u.toast('获取定位失败')
				})
			// } else {
				// uni.navigateTo({
				// 	url: '/subPages/login/login',
				// 	events: {
				// 		getLocation: data => {
				// 			// 传经纬度获取当前地址ID
				// 			this.$getLocation((lng, lat) => {
				// 				this.getarea(lng, lat)
				// 				// console.log(e);
				// 			}, (err) => {
				// 				// console.log(err);
				// 				this.$u.toast('获取定位失败')
				// 			})
				// 		}
				// 	}
				// })
			// }
		},
		onShow() {
			uni.$emit('displayBoxEmit')
			this.getcategory()
		},
		components: {
			NavbarHead,
			homeSwiper,
			displayBox
		},
		// 页面滚动
		onPageScroll(e) {
			let scrollTop = e.scrollTop
			if (scrollTop > 20) {
				this.bgColor = 'rgba(255, 255, 255, 1)'
			} else {
				this.bgColor = 'rgba(16, 34, 80, 0)'
			}
		},
		// 页面滚动到底部
		async onReachBottom() {
			if (this.page < this.last_page) {
				this.page++
				let res = await this.$api.getcateshop({
					cate: 1, // 一级分类ID
					// area: this.areaData.id, //城市ID
					two_cate: this.twoCate, //二级分类ID
					page: this.page, //页数
					total: 6 //条数
				})
				this.dataList.push(...res.data.data)
			} else {
				this.$u.toast('已经到底了！')
				this.loadShow = false
			}
		},
		methods: {
			// 跳转
			toUrl(url) {
				uni.navigateTo({
					url
				})
			},
			// 地址点击
			areaClick() {
				uni.navigateTo({
					url: `/subPages/city/city?id=${this.areaData.id}`,
					events: {
						areaBack: data => {
							this.areaData = data
							// console.log(data);
						}
					}
				})
			},
			// 专区点击跳转分类
			oneIdClick() {
				getApp().globalData.activeId = 1
				uni.switchTab({
					url: '/pages/classify/classify'
				})
			},
			// 添加到购物车点击
			async addCart(id) {
				let {
					msg
				} = await this.$api.addCart({
					goods_id: id,
					num: 1
				})
				this.$u.toast(msg)
			},
			// 获取热卖分类列表
			async getcategory() {
				let res = await this.$api.getcategory({
					cate: 1
				})
				this.tabsList = res.data
				// 获取默认商品列表
				this.getcateshop(res.data[0].id)
				// console.log(res);
			},
			// 获取商品
			async getcateshop(two_cate) {
				this.twoCate = two_cate
				this.page = 1
				let res = await this.$api.getcateshop({
					cate: 1, // 一级分类ID
					// area: this.areaData.id, //城市ID   商品不需要定位id获取
					two_cate, //二级分类ID
					page: 1, //页数
					total: 6 //条数
				})
				this.dataList = res.data.data
				this.last_page = res.data.last_page
				if (res.data.data.length == 0) this.loadShow = false
				else this.loadShow = true
			},
			// 获取当前所在区域
			async getarea(lng, lat) {
				let res = await this.$api.getarea({
					lng,
					lat
				})
				this.areaData = res.data
			},
		}
	}
</script>

<style>
	page {
		width: 100%;
		overflow-x: hidden;
	}
</style>
<style lang="scss" scoped>

	.topBox {
		width: 100%;
		// background-color: #e3c8fd;
		// background-image: linear-gradient(180deg, #e3c8fd 0%, #ffffff 100%);
		background-color: transparent;
		box-sizing: border-box;
		padding: 0 24rpx;
	}

	.topBox::before {
		z-index: -9;
		content: "";
		position: absolute;
		width: 470rpx;
		height: 420rpx;
		border-bottom-left-radius: 100rpx;
		background-color: #D4C6FF;
		filter: blur(24px);
		right: 0;
		top: 0;
	}

	.topBox::after {
		z-index: -10;
		content: "";
		position: absolute;
		width: 400rpx;
		height: 510rpx;
		border-bottom-right-radius: 100rpx;
		background-color: #F8D5F6;
		filter: blur(8px);
		top: 0;
		left: 0;
	}

	.searchBox {
		box-sizing: border-box;
		width: 100%;
		height: 84rpx;
		line-height: 84rpx;
		border-radius: 60rpx;
		opacity: 1;
		background: rgba(255, 255, 255, 1);
		border: 2px solid transparent;
		border-radius: 99px;
		background-clip: padding-box, border-box;
		background-origin: padding-box, border-box;
		background-image: linear-gradient(to right, #fff, #fff),
			linear-gradient(to right, #FFD1D6, #EECFFD);
		color: rgba(0, 0, 0, 0.8);
		font-size: 28rpx;
		font-weight: 1000;
		font-family: "PingFang SC";
		text-align: left;
		letter-spacing: 0.6rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;

		image {
			width: 32rpx;
			height: 32rpx;
			vertical-align: middle;
		}

		.search {
			flex: 1;
		}

		.area {
			padding: 0 20rpx;
			box-sizing: border-box;
			text-align: right;
		}
	}

	.swiper {
		margin-top: 20rpx;
		box-sizing: border-box;
		width: 100%;
		height: 320rpx;
		border-radius: 24rpx;
		opacity: 1;
		background-color: #FFD1D6;
	}

	.displayBox {
		margin-top: 8rpx;
		box-sizing: border-box;
		width: 100%;
		padding: 0 24rpx;


	}

	.listTop {
		margin-top: 24rpx;
		padding: 0 20rpx;
		box-sizing: border-box;

		&-title {
			width: 100%;
			height: 72rpx;

			image {
				width: 100%;
				height: 72rpx;
			}
		}

		&-tabs {
			white-space: nowrap;
			width: 100%;
			margin: 20rpx 0;
			padding: 0 12rpx;
			box-sizing: border-box;
			// display: flex;
			// justify-content: space-around;
			// align-items: center;

			text {
				display: inline-block;
				box-sizing: border-box;
				margin: 0 12rpx;
				padding: 7rpx 35rpx;
				border-radius: 35rpx;
				opacity: 1;
				background: #fff;
				color: $textColor;
				font-size: 25rpx;
				font-weight: 400;
			}

			.textActive {
				color: #fff;
				background-color: $boxBg;
			}
		}
	}



	.listBox {
		margin-top: 8rpx;
		box-sizing: border-box;
		width: 100%;
		padding: 0 24rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-wrap: wrap;
	}

	.itemBox {
		width: 344rpx;
		border-radius: 24rpx;
		overflow: hidden;
		background: rgba(255, 255, 255, 1);
		padding-bottom: 20rpx;
		box-sizing: border-box;
		margin-bottom: 14rpx;



		.ship {
			width: 100%;
			height: 344rpx;
			vertical-align: top;
		}

		&-name {
			box-sizing: border-box;
			padding: 0 20rpx;
			margin-top: 20rpx;
			width: 100%;
			// height: 80rpx;
			color: rgba(0, 0, 0, 0.8);
			font-size: 26rpx;
			font-weight: 500;
		}

		&-price {
			box-sizing: border-box;
			padding: 0 20rpx;
			margin-top: 8rpx;
			height: 28rpx;
			line-height: 28rpx;
			color: rgba(0, 0, 0, 0.4);
			font-size: 22rpx;
			font-weight: 400;
		}

		&-addCart {
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-sizing: border-box;
			padding: 0 20rpx;

			.left {
				color: $textColor;
				font-size: 22rpx;
				font-weight: 400;
				font-family: "PingFang SC";
				text-align: left;
			}

			.right {
				image {
					width: 40rpx;
					height: 40rpx;
					// background-color: $boxBg;
					border-radius: 50%;
				}
			}
		}
	}
</style>